package io.plus.interactive.user_interface;

import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.interactive.Configuration;
import io.plus.interactive.exception.AioDecodeException;

import java.nio.ByteBuffer;

public interface AioHandler {


    /**
     * 编码
     */
    ByteBuffer encode(Configuration configuration, AsynchronousSocketChannelContext asynchronousSocketChannelContext, Packet packet);

    /**
     * 根据ByteBuffer解码成业务需要的Packet对象.
     * 如果收到的数据不全，导致解码失败，请返回null，在下次消息来时框架层会自动续上前面的收到的数据
     *
     * @param asynchronousSocketChannelContext
     * @param buffer                           参与本次希望解码的ByteBuffer
     * @param limit                            ByteBuffer的limit
     * @param position                         ByteBuffer的position，不一定是0哦
     * @param readableLength                   ByteBuffer参与本次解码的有效数据（= limit - position）
     */
    Packet decode(AsynchronousSocketChannelContext asynchronousSocketChannelContext, ByteBuffer buffer, int limit, int position, int readableLength) throws AioDecodeException;

    /**
     * 处理消息包
     */
    void handler(AsynchronousSocketChannelContext asynchronousSocketChannelContext, Packet packet) throws Exception;

}
